/*
 * AccountFacade.java
 *
 * Created on 8 2007  20:59
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.persistent.model.facade;

import com.persistent.entity.Account;
import com.persistent.entity.Authentification;
import com.persistent.model.AbstractFacadeImpl;
import java.sql.Date;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import static server.authentification.module.crypto.UserPasswordCheck.putIntoDateBase;
import java.util.List;

/**
 *
 * @author alexog
 */
public class AccountFacade extends AbstractFacadeImpl<Account>{       
    
    private volatile String password;
    
    /** Creates a new instance of AccountFacade */
    public AccountFacade() {
        super();
    }

    public void create(Account account) {
        etx.begin();
        Authentification auth = new Authentification();       
        auth.setUsername(account.getUsername());
        auth.setPassword(this.generateAccountPassword(account.getUsername(),password));
        auth.setAccount(account);               
        account.setCreationDate(new Date(System.currentTimeMillis()));
        em.persist(auth);
        em.persist(account);
        
        em.flush();      
        etx.commit();
    }

    public Account find(Account v) {
        return (Account) em.find(Account.class, v);
    }

    public List<Account> findAll() {
        return em.createQuery("select object(o) from Account as o").getResultList();
    }

    private String generateAccountPassword(String username,String password) {
        return putIntoDateBase(username, password);
    }
    
    public void setUserPassword(String password){
        this.password = password;
    }
    
}
